//
//  NSStringAdditions.m
//  Weibo
//
//  Created by junmin liu on 10-9-29.
//  Copyright 2010 Openlab. All rights reserved.
//

#import "NSString+Additions.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Additions)

//+ (NSString *)generateGuid {
//	CFUUIDRef	uuidObj = CFUUIDCreate(nil);//create a new UUID
//	//get the string representation of the UUID
//
//#if __has_feature(objc_arc)
//	NSString	*uuidString = (__bridge_transfer  NSString*)CFUUIDCreateString(nil, uuidObj);
//#else
//    NSString	*uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);
//#endif
//	return uuidString;
//}

///////////////////////////////////////////////////////////////////////////////////////////////////
//- (BOOL)isWhitespaceAndNewlines {
//	NSCharacterSet* whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
//	for (NSInteger i = 0; i < self.length; ++i) {
//		unichar c = [self characterAtIndex:i];
//		if (![whitespace characterIsMember:c]) {
//			return NO;
//		}
//	}
//	return YES;
//}


///////////////////////////////////////////////////////////////////////////////////////////////////
//- (BOOL)isEmptyOrWhitespace {
//	return !self.length ||
//	![self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length;
//}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Copied and pasted from http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg28175.html
//- (NSDictionary*)queryDictionaryUsingEncoding:(NSStringEncoding)encoding {
//	NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;"];
//	NSMutableDictionary* pairs = [NSMutableDictionary dictionary];
//	NSScanner* scanner = [[[NSScanner alloc] initWithString:self] autorelease];
//	while (![scanner isAtEnd]) {
//		NSString* pairString = nil;
//		[scanner scanUpToCharactersFromSet:delimiterSet intoString:&pairString];
//		[scanner scanCharactersFromSet:delimiterSet intoString:NULL];
//		NSArray* kvPair = [pairString componentsSeparatedByString:@"="];
//		if (kvPair.count == 2) {
//			NSString* key = [[kvPair objectAtIndex:0]
//							 stringByReplacingPercentEscapesUsingEncoding:encoding];
//			NSString* value = [[kvPair objectAtIndex:1]
//							   stringByReplacingPercentEscapesUsingEncoding:encoding];
//			[pairs setObject:value forKey:key];
//		}
//	}
//	
//	return [NSDictionary dictionaryWithDictionary:pairs];
//}


///////////////////////////////////////////////////////////////////////////////////////////////////
//- (NSString*)stringByAddingQueryDictionary:(NSDictionary*)query {
//	NSMutableArray* pairs = [NSMutableArray array];
//	for (NSString* key in [query keyEnumerator]) {
//		NSString* value = [query objectForKey:key];
//		value = [value stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
//		value = [value stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
//		NSString* pair = [NSString stringWithFormat:@"%@=%@", key, value];
//		[pairs addObject:pair];
//	}
//	
//	NSString* params = [pairs componentsJoinedByString:@"&"];
//	if ([self rangeOfString:@"?"].location == NSNotFound) {
//		return [self stringByAppendingFormat:@"?%@", params];
//	} else {
//		return [self stringByAppendingFormat:@"&%@", params];
//	}
//}


///////////////////////////////////////////////////////////////////////////////////////////////////
//- (NSComparisonResult)versionStringCompare:(NSString *)other {
//	NSArray *oneComponents = [self componentsSeparatedByString:@"a"];
//	NSArray *twoComponents = [other componentsSeparatedByString:@"a"];
//	
//	// The parts before the "a"
//	NSString *oneMain = [oneComponents objectAtIndex:0];
//	NSString *twoMain = [twoComponents objectAtIndex:0];
//	
//	// If main parts are different, return that result, regardless of alpha part
//	NSComparisonResult mainDiff;
//	if ((mainDiff = [oneMain compare:twoMain]) != NSOrderedSame) {
//		return mainDiff;
//	}
//	
//	// At this point the main parts are the same; just deal with alpha stuff
//	// If one has an alpha part and the other doesn't, the one without is newer
//	if ([oneComponents count] < [twoComponents count]) {
//		return NSOrderedDescending;
//	} else if ([oneComponents count] > [twoComponents count]) {
//		return NSOrderedAscending;
//	} else if ([oneComponents count] == 1) {
//		// Neither has an alpha part, and we know the main parts are the same
//		return NSOrderedSame;
//	}
//	
//	// At this point the main parts are the same and both have alpha parts. Compare the alpha parts
//	// numerically. If it's not a valid number (including empty string) it's treated as zero.
//	NSNumber *oneAlpha = [NSNumber numberWithInt:[[oneComponents objectAtIndex:1] intValue]];
//	NSNumber *twoAlpha = [NSNumber numberWithInt:[[twoComponents objectAtIndex:1] intValue]];
//	return [oneAlpha compare:twoAlpha];
//}
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
//- (NSString*)md5Hash {
//	return [[self dataUsingEncoding:NSUTF8StringEncoding] md5Hash];
//}

-(CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (NSString *)URLEncodedString 
{
#if __has_feature(objc_arc)
    NSString *result = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (__bridge CFStringRef)self,
                                                                           NULL,
																		   CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                                    kCFStringEncodingUTF8);
#else
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                   (CFStringRef)self,
                                                    NULL,
                                                   CFSTR("!*'();:@&=+$,/?%#[]"),
                                                   kCFStringEncodingUTF8);
#endif
	return result;
}

- (NSString*)stringByReplacingUnicode
{
        NSString *tempStr1 = [self stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"]; 
        NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""]; 
        NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""]; 
        NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding]; 
        NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData 
                                                              mutabilityOption:NSPropertyListImmutable  
                                                                        format:NULL 
                                                              errorDescription:NULL]; 
        
        return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"]; 
}

- (NSString*)stringByTrimmingWhiteChars
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
@end

@implementation NSString(MD5Addition)

- (NSString *) MD5String
{
    const char *original_str = [self UTF8String];
	unsigned char result[32];
	CC_MD5(original_str, strlen(original_str), result);//调用md5
	NSMutableString *hash = [NSMutableString string];
	for (int i = 0; i < 16; i++){
        [hash appendFormat:@"%02x", result[i]];
	}
    
	return hash;//全部大写
}

- (NSString *)uppercaseMD5String
{
    const char *original_str = [self UTF8String];
	unsigned char result[32];
	CC_MD5(original_str, strlen(original_str), result);//调用md5
	NSMutableString *hash = [NSMutableString string];
	for (int i = 0; i < 16; i++){
        [hash appendFormat:@"%02X", result[i]];
	}
    
	return [hash uppercaseString];//全部大写
}


- (NSString*)encryptedString
{
    return [[self uppercaseMD5String] uppercaseMD5String];
}
@end

@implementation NSString (JSONAccess)

- (id)JSONObject
{
    NSError *error = nil;
    
    NSData *stringData = [self dataUsingEncoding:NSUTF8StringEncoding];
    
    id jsonObj = [NSJSONSerialization JSONObjectWithData:stringData
                                                        options:NSJSONReadingAllowFragments 
                                                          error:&error];
    
    if (error != nil)
    {
        @throw [NSException exceptionWithName:@"JSON error"
                                       reason:[error description]
                                     userInfo:@{@"string":[NSString stringWithString:self]}];
    }
    
    return jsonObj;
    
}

@end

//static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 

//@implementation NSString (NSStringUtils)
//- (NSString*)encodeAsURIComponent
//{
//	const char* p = [self UTF8String];
//	NSMutableString* result = [NSMutableString string];
//	
//	for (;*p ;p++) {
//		unsigned char c = *p;
//		if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '-' || c == '_') {
//			[result appendFormat:@"%c", c];
//		} else {
//			[result appendFormat:@"%%%02X", c];
//		}
//	}
//	return result;
//}

//+ (NSString*)base64encode:(NSString*)str 
//{
//    if ([str length] == 0)
//        return @"";
//    
//    const char *source = [str UTF8String];
//    int strlength  = strlen(source);
//    
//    char *characters = malloc(((strlength + 2) / 3) * 4);
//    if (characters == NULL)
//        return nil;
//    
//    NSUInteger length = 0;
//    NSUInteger i = 0;
//    
//    while (i < strlength) {
//        char buffer[3] = {0,0,0};
//        short bufferLength = 0;
//        while (bufferLength < 3 && i < strlength)
//            buffer[bufferLength++] = source[i++];
//        characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
//        characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
//        if (bufferLength > 1)
//            characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
//        else characters[length++] = '=';
//        if (bufferLength > 2)
//            characters[length++] = encodingTable[buffer[2] & 0x3F];
//        else characters[length++] = '=';
//    }
//    
//    return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
//}

//- (NSString*)escapeHTML
//{
//	NSMutableString* s = [NSMutableString string];
//	
//	int start = 0;
//	int len = [self length];
//	NSCharacterSet* chs = [NSCharacterSet characterSetWithCharactersInString:@"<>&\""];
//	
//	while (start < len) {
//		NSRange r = [self rangeOfCharacterFromSet:chs options:0 range:NSMakeRange(start, len-start)];
//		if (r.location == NSNotFound) {
//			[s appendString:[self substringFromIndex:start]];
//			break;
//		}
//		
//		if (start < r.location) {
//			[s appendString:[self substringWithRange:NSMakeRange(start, r.location-start)]];
//		}
//		
//		switch ([self characterAtIndex:r.location]) {
//			case '<':
//				[s appendString:@"&lt;"];
//				break;
//			case '>':
//				[s appendString:@"&gt;"];
//				break;
//			case '"':
//				[s appendString:@"&quot;"];
//				break;
//			case '&':
//				[s appendString:@"&amp;"];
//				break;
//		}
//		
//		start = r.location + 1;
//	}
//	
//	return s;
//}
//
//- (NSString*)unescapeHTML
//{
//	NSMutableString* s = [NSMutableString string];
//	NSMutableString* target = [[self mutableCopy] autorelease] ;
//	NSCharacterSet* chs = [NSCharacterSet characterSetWithCharactersInString:@"&"];
//	
//	while ([target length] > 0) {
//		NSRange r = [target rangeOfCharacterFromSet:chs];
//		if (r.location == NSNotFound) {
//			[s appendString:target];
//			break;
//		}
//		
//		if (r.location > 0) {
//			[s appendString:[target substringToIndex:r.location]];
//			[target deleteCharactersInRange:NSMakeRange(0, r.location)];
//		}
//		
//		if ([target hasPrefix:@"&lt;"]) {
//			[s appendString:@"<"];
//			[target deleteCharactersInRange:NSMakeRange(0, 4)];
//		} else if ([target hasPrefix:@"&gt;"]) {
//			[s appendString:@">"];
//			[target deleteCharactersInRange:NSMakeRange(0, 4)];
//		} else if ([target hasPrefix:@"&quot;"]) {
//			[s appendString:@"\""];
//			[target deleteCharactersInRange:NSMakeRange(0, 6)];
//		} else if ([target hasPrefix:@"&amp;"]) {
//			[s appendString:@"&"];
//			[target deleteCharactersInRange:NSMakeRange(0, 5)];
//		} else {
//			[s appendString:@"&"];
//			[target deleteCharactersInRange:NSMakeRange(0, 1)];
//		}
//	}
//	
//	return s;
//}

//+ (NSString*)localizedString:(NSString*)key
//{
//	return [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:key];
//}

//@end

@implementation NSString (LLFile)

+ (NSString *)contentStringWithFileName:(NSString *)fileName
{
    NSString *bundleName = @"walletResources.bundle";
    
    NSString *filePath = [NSString stringWithFormat:@"%@/Contents/Resources/%@", bundleName, fileName];
    
    NSError *error = nil;
    
    NSString *content = [NSString stringWithContentsOfFile:filePath
                                                  encoding:NSUTF8StringEncoding
                                                     error:&error];
    
    if (content.length > 0) {
        return content;
    }
    
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: bundleName];
    NSBundle *sdkBundle = [NSBundle bundleWithPath: bundlePath];
    if (sdkBundle != nil && fileName)
    {
        filePath = [[sdkBundle resourcePath] stringByAppendingPathComponent:fileName];
        
        content = [NSString stringWithContentsOfFile:filePath
                                            encoding:NSUTF8StringEncoding
                                               error:&error];
        
        if (content.length > 0) {
            return content;
        }
    }
    
	filePath = [[NSBundle mainBundle] bundlePath];
	//filePath = [filePath stringByDeletingLastPathComponent];
	NSString *absolutefileName = [fileName lastPathComponent];
	//	TRACELOG([filePath stringByAppendingPathComponent:absolutefileName]);
	absolutefileName = [filePath stringByAppendingPathComponent:absolutefileName];
    
    content = [NSString stringWithContentsOfFile:absolutefileName
                                        encoding:NSUTF8StringEncoding
                                           error:nil];
    
    return content;
}

@end

@implementation NSString (LLFormatter)

- (NSString*)formatMaxLen:(int)maxLen
{
    if (self.length > maxLen) 
    {
        return [self substringToIndex:maxLen];
    }
    return self;
}

- (NSString*)formattedCardNo
{
    NSMutableString *toBeString = [NSMutableString stringWithString:self];
    
    [toBeString replaceOccurrencesOfString:@" " withString:@"" 
                                   options:NSCaseInsensitiveSearch range:NSMakeRange(0, toBeString.length)];
    
    for (int i = 6; i > 0; i--) 
    {
        int insertSpaceIndex = i * 4;
        if ([toBeString length] > insertSpaceIndex)
        {
            [toBeString insertString:@" " atIndex:insertSpaceIndex];
        }
    }
    
    int maxLen = 23;
    if (toBeString.length > maxLen) 
    {
        [toBeString deleteCharactersInRange:NSMakeRange(maxLen, toBeString.length - maxLen)];
    }
    
    return [toBeString formatMaxLen:maxLen];
}

- (NSString*)unformattedCardNo
{
    return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}

- (NSString*)formattedPhoneNo
{
    NSMutableString *toBeString = [NSMutableString stringWithString:self];
    
    [toBeString replaceOccurrencesOfString:@" " withString:@""
                                   options:NSCaseInsensitiveSearch range:NSMakeRange(0, toBeString.length)];
    
    
    if ([toBeString length] > 7)
    {
        [toBeString insertString:@"-" atIndex:7];
    }
    if ([toBeString length] > 3)
    {
        [toBeString insertString:@"-" atIndex:3];
    }
    
    int maxLen = 13;
    if (toBeString.length > maxLen) 
    {
        [toBeString deleteCharactersInRange:NSMakeRange(maxLen, toBeString.length - maxLen)];
    }
    
    return [toBeString formatMaxLen:maxLen];
}

- (NSString*)unformattedPhoneNo
{
    return [self stringByReplacingOccurrencesOfString:@"-" withString:@""];
}


- (NSString*)formattedIdcardNo
{
    NSMutableString *toBeString = [NSMutableString stringWithString:self];
    
    [toBeString replaceOccurrencesOfString:@" " withString:@"" 
                                   options:NSCaseInsensitiveSearch range:NSMakeRange(0, toBeString.length)];
    
    
    if ([toBeString length] > 14)
    {
        [toBeString insertString:@" " atIndex:14];
    }
    if ([toBeString length] > 6)
    {
        [toBeString insertString:@" " atIndex:6];
    }
    
    int maxLen = 20;
    if (toBeString.length > maxLen) 
    {
        [toBeString deleteCharactersInRange:NSMakeRange(maxLen, toBeString.length - maxLen)];
    }
    
    return [toBeString formatMaxLen:maxLen];
}

- (NSString*)unformattedIdcardNo
{
    return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}

- (NSString*)formatDate
{
    NSMutableString *dateString = [NSMutableString stringWithString:[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    
    if (self.length > 12) 
    {
        [dateString insertString:@":" atIndex:12];
    }
    if (self.length > 10) 
    {
        [dateString insertString:@":" atIndex:10];
    }
    if (self.length > 8) 
    {
        [dateString insertString:@" " atIndex:8];
    }
    if (self.length > 6) 
    {
        [dateString insertString:@"-" atIndex:6];
    }
    if (self.length > 4) 
    {
        [dateString insertString:@"-" atIndex:4];
    }
    
    [dateString deleteCharactersInRange:NSMakeRange(dateString.length - 3, 3)];     // 移除后三位
    return dateString;
}

- (BOOL)isPureNumber
{
    NSScanner *scanner = [NSScanner scannerWithString:self];
    
    int number;
    return [scanner scanInt:&number] && [scanner isAtEnd];
}

- (BOOL)isPureCharacter
{
    NSCharacterSet *letterSet = [NSCharacterSet letterCharacterSet];
    return  [[self stringByTrimmingCharactersInSet:letterSet] isEqualToString:@""];
}

- (NSString*)secureMobile
{
    return [NSString stringWithFormat:@"%@****%@", 
            [self substringToIndex:3],
            [self substringFromIndex:self.length-4]];
}

- (NSString*)secureStringWithShow:(int)headChars andTailsChrs:(int)tailChars
{
    if (headChars + tailChars >= self.length) {
        return [NSString stringWithString:self];
    }
    
    NSMutableString *maskString = [NSMutableString string];
    
    for (int i = 0; i < self.length - (headChars + tailChars); i++) {
        [maskString appendString:@"*"];
    }
    
    [maskString insertString:[self substringToIndex:headChars] atIndex:0];
    [maskString appendString:[self substringFromIndex:self.length-tailChars]];
    
    return maskString;
}

- (BOOL)isOnlyContainCharactersString:(NSString*)characters
{
    NSScanner* scanner = [[NSScanner alloc] initWithString:self];
    NSCharacterSet* charsets = [NSCharacterSet characterSetWithCharactersInString:characters];
    NSString* restr = @"";
    
    [scanner scanCharactersFromSet:charsets intoString:&restr];
    
    if([restr length] == [self length])
    {
        return YES;
    }
    return  NO;
    
}

-(NSString *)secureName{
    return [NSString stringWithFormat:@"*%@",[self substringFromIndex:1]];
}

- (NSString *)clearWhiteSpaceAndNewLine
{//去除一个String里面的空格跟换行
	return [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
			componentsJoinedByString:@""];
}

- (BOOL)isValidRechargeMoney
{
    NSRange range = [self rangeOfString:@"."];
    if (range.length > 0) {
        NSString *behindString = [self substringFromIndex:range.location+range.length];
        NSLog(@"behindString: %@",behindString);
        if (behindString.length > 2) {
            return NO;
        }
    }

    return YES;
}

- (BOOL)isValidNameString
{
    if ([self length]<2 || [self length]>7) {
        return NO;
    }
    for (int i = 0; i < [self length]; i++) {
        int a = [self characterAtIndex:i];
        NSLog(@"a lenght: %d",a);
        if( a < 0x4e00 || a > 0x9fff)
        {
            return NO;
        }
    }

    return YES;
}

- (BOOL)isVerifiedPassword
{
    if (self.length >20 || self.length <6) {
        return NO;
    }
//    if ([self isPureNumber]) {
//        return NO;
//    }
//    if ([self isPureCharacter]) {
//        return NO;
//    }
    return YES;
}

- (BOOL)isValidMobile
{
    if (self.length != 11) {
        return NO;
    }
    if (![self isPureNumber]) {
        return NO;
    }
    if (![self hasPrefix:@"1"]) {
        
        return NO;
    }
    return YES;
}
@end

@implementation NSString (Chk18PaperId)
/**
 * 功能:获取指定范围的字符串
 * 参数:字符串的开始小标
 * 参数:字符串的结束下标
 */
+(NSString *)getStringWithRange:(NSString *)str Value1:(NSInteger  )value1 Value2:(NSInteger )value2;
{
    return [str substringWithRange:NSMakeRange(value1,value2)];
}
/**
 * 功能:判断是否在地区码内
 * 参数:地区码
 */
+(BOOL)areaCode:(NSString *)code
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:@"北京" forKey:@"11"];
    [dic setObject:@"天津" forKey:@"12"];
    [dic setObject:@"河北" forKey:@"13"];
    [dic setObject:@"山西" forKey:@"14"];
    [dic setObject:@"内蒙古" forKey:@"15"];
    [dic setObject:@"辽宁" forKey:@"21"];
    [dic setObject:@"吉林" forKey:@"22"];
    [dic setObject:@"黑龙江" forKey:@"23"];
    [dic setObject:@"上海" forKey:@"31"];
    [dic setObject:@"江苏" forKey:@"32"];
    [dic setObject:@"浙江" forKey:@"33"];
    [dic setObject:@"安徽" forKey:@"34"];
    [dic setObject:@"福建" forKey:@"35"];
    [dic setObject:@"江西" forKey:@"36"];
    [dic setObject:@"山东" forKey:@"37"];
    [dic setObject:@"河南" forKey:@"41"];
    [dic setObject:@"湖北" forKey:@"42"];
    [dic setObject:@"湖南" forKey:@"43"];
    [dic setObject:@"广东" forKey:@"44"];
    [dic setObject:@"广西" forKey:@"45"];
    [dic setObject:@"海南" forKey:@"46"];
    [dic setObject:@"重庆" forKey:@"50"];
    [dic setObject:@"四川" forKey:@"51"];
    [dic setObject:@"贵州" forKey:@"52"];
    [dic setObject:@"云南" forKey:@"53"];
    [dic setObject:@"西藏" forKey:@"54"];
    [dic setObject:@"陕西" forKey:@"61"];
    [dic setObject:@"甘肃" forKey:@"62"];
    [dic setObject:@"青海" forKey:@"63"];
    [dic setObject:@"宁夏" forKey:@"64"];
    [dic setObject:@"新疆" forKey:@"65"];
    [dic setObject:@"台湾" forKey:@"71"];
    [dic setObject:@"香港" forKey:@"81"];
    [dic setObject:@"澳门" forKey:@"82"];
    [dic setObject:@"国外" forKey:@"91"];
    
    if ([dic objectForKey:code] == nil) {
        
        return NO;
    }
    return YES;
}

/**
 * 功能:验证身份证是否合法
 * 参数:输入的身份证号
 */
+(BOOL) Chk18PaperId:(NSString *)sPaperId
{
    // x 变成 X
    sPaperId = [sPaperId uppercaseString];
    
    //判断位数
    if ([sPaperId length] < 15 ||[sPaperId length] > 18) {
        
        return NO;
    }
    
    NSString *carid = sPaperId;
    long lSumQT =0;
    //加权因子
    int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
    //校验码
    unsigned char sChecker[11]={'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};
    
    //将15位身份证号转换成18位
    
    NSMutableString *mString = [NSMutableString stringWithString:sPaperId];
    if ([sPaperId length] == 15) {
        
        
        [mString insertString:@"19" atIndex:6];
        
        long p = 0;
        const char *pid = [mString UTF8String];
        for (int i=0; i<=16; i++)
        {
            p += (pid[i]-48) * R[i];
        }
        
        int o = p%11;
        NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];
        [mString insertString:string_content atIndex:[mString length]];
        carid = mString;
        
    }
    
    //判断地区码
    NSString * sProvince = [carid substringToIndex:2];
    
    if (![NSString areaCode:sProvince]) {
        
        return NO;
    }
    
    //判断年月日是否有效
    
    //年份
    int strYear = [[NSString getStringWithRange:carid Value1:6 Value2:4] intValue];
    //月份
    int strMonth = [[NSString getStringWithRange:carid Value1:10 Value2:2] intValue];
    //日
    int strDay = [[NSString getStringWithRange:carid Value1:12 Value2:2] intValue];
    
    
    NSTimeZone *localZone = [NSTimeZone localTimeZone];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeZone:localZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]];
    if (date == nil) {
        
        return NO;
    }
    
    const char *PaperId  = [carid UTF8String];
    
    //检验长度
    if( 18 != strlen(PaperId))
    {
        return NO;
    }
    
    //校验数字
    for (int i=0; i<18; i++)
    {
        if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) )
        {
            return NO;
        }
    }
    //验证最末的校验码
    for (int i=0; i<=16; i++)
    {
        lSumQT += (PaperId[i]-48) * R[i];
    }
    if (sChecker[lSumQT%11] != PaperId[17] )
    {
        return NO;
    }
    
    return YES;
} 

@end

@implementation NSString (createTime)

- (NSString *)formattedCreateOidTime
{
    if (self.length < 12) {
        return nil;
    }
    NSString *ret = nil;
    @try {
        NSString *year = [self substringWithRange:NSMakeRange(0, 4)];
        NSString *mon = [self substringWithRange:NSMakeRange(4, 2)];
        NSString *day = [self substringWithRange:NSMakeRange(6, 2)];
        NSString *HH = [self substringWithRange:NSMakeRange(8, 2)];
        NSString *MM = [self substringWithRange:NSMakeRange(10, 2)];
        NSString *SS = [self substringWithRange:NSMakeRange(12, 2)];
        
        ret = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",year,mon,day,HH,MM,SS];
    }
    @catch (NSException *exception) {
        ret = @"";
    }
    @finally {
    }

    return ret;
}

@end

@implementation NSString (UUID)

- (NSString*)UUIDString
{
    
    CFUUIDRef uuidObject = CFUUIDCreateFromString(kCFAllocatorDefault, (CFStringRef)self);
    
    // Create universally unique identifier (object)
    //CFUUIDCreate(kCFAllocatorDefault);
    
    // Get the string representation of CFUUID object.
    NSString *uuidStr = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuidObject));
    
    // If needed, here is how to get a representation in bytes, returned as a structure
    // typedef struct {
    //   UInt8 byte0;
    //   UInt8 byte1;
    //   ...
    //   UInt8 byte15;
    // } CFUUIDBytes;
    //CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidObject);
    
    CFRelease(uuidObject);
    
    return uuidStr;
}

@end